// const util = require("./../../../utils/util.js");
// const http = require("./../../../utils/http.js");

const app = getApp();
// let genderArray = ['男', '女', '人妖', '未知生物'];


Page({
    data: {
        USERRECORD: {},
        UPLOAD_IMAGE: app.globalData.UPLOAD_IMAGE,
        startFooterLoading: true,
        saleFriendList: [],
        currentTime: '',
        pageSize: 10,
        pageNo: 1,
        initPageNo: 1,
        // startFooterLoading:false,
        // showGeMoreLoadin: false,
        // notDataTips: false, //上拉加载没有更多数据
        newMessage: false,
        newMessageNumber: 0,
        select: 1,

        leftList: [],
        rightList: [],
        leftHeight: 0,
        rightHeigt: 1,
    },
    onLoad: function () {
        const that = this
        // wx.showLoading({
        //   title: '加载中',
        // });
        that.setData({USERRECORD: wx.getStorageSync('ALLINFO').bbsUserRecord})
        that.getList();

        //设置当前时间
        that.setData({
            // currentTime: util.formatTime(new Date())
        });
    },

    onShow: function () {
        const that = this
        // let type = 0;
        // http.getNewInbox(type,res=> {
        //   if (res.data.data != 0 && res.data.data != null && res.data.data != '') {
        //     this.setData({
        //       newMessage: true,
        //       newMessageNumber: res.data.data
        //     });
        //   } else {
        //     this.setData({
        //       newMessage: false,
        //       newMessageNumber: 0
        //     });
        //   }
        // });

        if (app.globalData.needReloadSaleFriend) {
            app.globalData.needReloadSaleFriend = false;
            that.setData({
                leftList: [],
                rightList: [],
                leftHeight: 0,
                rightHeigt: 1,
                pageNo: 1
            })
            that.getList();
        }

    },
    onShareAppMessage: function (res) {
        const that = this
        // if (res.from === 'button') {
        //     // 来自页面内转发按钮
        //     console.log(res.target)
        // }
        // return {
        //     title: '喜欢ta，那就说出来吧',
        //     path: '/pages/index/index',
        //     imageUrl: 'http://img.qiuhuiyi.cn/sale_friend_bg.jpg',
        //     success: function (res) {
        //         // 转发成功
        //     },
        //     fail: function (res) {
        //         // 转发失败
        //     }
        // }
        return {
            title: '喜欢ta，那就说出来吧',
            path: '/pages/index/index?regionCode=' + that.data.USERRECORD.regionCode,
            // imageUrl:
        }
    },

    /**
     * 获取具体类型的贴子
     */
    selected: function (e) {
        const that = this
        let currentSelect = e.target.dataset.type;

        that.setData({
            pageNo: 1,
            leftList: [],
            rightList: [],
            leftHeight: 0,
            rightHeigt: 1,

            select: currentSelect,
            sales: []
        });
        that.getList();
    },

    /**
     * 进入发表页面
     */
    post: function () {
        const accountState = app.globalData.accountState
        const that = this
        //查询用户授权状态
        if (app.globalData.HASUSERINFO) {
            if (accountState.isDisable) {
                wx.showToast({
                    title: `您的账号已被${accountState.stateName},剩余时间${accountState.disableDay}`,
                    icon: 'none'
                })
            } else {
                wx.navigateTo({
                    url: '/pages/components/sale/post_sale/post_sale'
                })
            }
        } else {
            app.getUserProfile()
        }

    },

    /**
     * 进入详情页面
     */
    comment: function (e) {
        let id = e.currentTarget.dataset.objid;
        console.log(e)
        wx.navigateTo({
            url: `/pages/components/sale/sale_detail/sale_detail?id=${id}`
        })
    },

    /**
     * 进入新消息列表
     */
    openMessage: function () {
        // wx.navigateTo({
        //   url: '/pages/personal/message/message?type=0&new_message=1'
        // })
    },

    /**
     * 获取贴子列表
     */
    getList: function () {

        const that = this;

        // let objType = this.data.select;
        // var order_by = 'created_at';
        // var sort_by = 'desc';
        // if (objType == 4) {
        //     order_by = 'praise_number';
        //     sort_by = 'desc';
        // }
        // if (this.data.postType == 3) {
        //     this.setData({
        //         pageNo: this.data.initPageNo
        //     });
        // }
        console.log(that.data)
        console.log("curretSelect:", that.data.select)
        //classIndex 卖舍友类型
        app.wxRequest('get', app.globalData.HOSTURL + "/bbs/bbsSaleFriends/wise/mini/fullList?pageSize=" + that.data.pageSize + "&pageNo=" + that.data.pageNo + "&classIndex=" + that.data.select, null).then(res => {
            console.log(res);

            let data = res.data.result.records;

            let leftList = that.data.leftList;
            let rightList = that.data.rightList;
            let leftHeight = that.data.leftHeight;
            let rightHeigt = that.data.rightHeigt;

            if (res.data.code == 200) {
                // wx.hideLoading();
                wx.stopPullDownRefresh()
                this.setData({
                    // showGeMoreLoadin: false
                    startFooterLoading: false
                });

                if (data.length > 0) {
                    data.forEach(item => {
                        console.log("left:", leftHeight)
                        console.log("right:", rightHeigt)
                        console.log("height", item.bbsSaleFriendsImageList[0]["height"])
                        if (leftHeight <= rightHeigt) {
                            // console.log("left:", leftHeight)
                            // console.log("在left添加")
                            leftList.push(item);
                            leftHeight += parseInt(item.bbsSaleFriendsImageList[0]["height"]) == undefined ? 0 : parseInt(item.bbsSaleFriendsImageList[0]["height"]);
                        } else {
                            // console.log("right:", rightHeigt)
                            // console.log("在right添加")
                            rightList.push(item)
                            rightHeigt += parseInt(item.bbsSaleFriendsImageList[0]["height"]) == undefined ? 0 : parseInt(item.bbsSaleFriendsImageList[0]["height"]);
                        }

                        this.setData({
                            leftList: leftList,
                            rightList: rightList,
                            leftHeight: leftHeight,
                            rightHeigt: rightHeigt,
                            saleFriendList: data
                        })
                    });

                    that.setData({
                        pageNo: that.data.pageNo + 1
                    });
                }
                app.getUserAllInfo().then(res => {
                    const userRecord = res.bbsUserRecord
                    that.setData({
                        USERRECORD: res.bbsUserRecord,


                    })
                    app.setTabbarBadge()
                })
                // else {
                //     this.setData({
                //         notDataTips: true   //没有更多数据
                //     });
                // }
            }


        })

    },

    /**
     * 下拉刷新，获取最新的贴子
     */
    onPullDownRefresh: function () {
        const that = this
        // this.getMostNewData();
        that.setData({
            leftList: [],
            rightList: [],
            leftHeight: 1,
            rightHeigt: 2,
            pageNo: 1

        })
        that.getList();
    },

    /**
     * 上拉加载跟多
     */
    onReachBottom: function (e) {
        const that = this
        that.setData({
            // notDataTips: false,
            // showGeMoreLoadin: true
            startFooterLoading: true
        });

        console.log('上拉')
        that.getList();
    },

    /**
     * 获取当前最新的贴子
     */
    getMostNewData: function () {
        // let time = this.data.currentTime;
        // http.get('/most_new_sale_friend?time='+time, {}, res => {
        //   let sales = this.data.sales;
        //   let data = res.data.data.map(item => {
        //     let ifRepeat = false;
        //     for(let sale of sales){
        //       if(sale.id == item.id){
        //         ifRepeat = true;
        //       }
        //     }
        //     if(!ifRepeat){
        //       sales.unshift(item);
        //     }
        //   });

        //   this.setData({
        //     sales:sales
        //     });
        //   wx.stopPullDownRefresh();
        //   this.setData({
        //     // currentTime: util.formatTime(new Date())
        //   });
        // });
    },

    /**
     * 点赞
     */
    // praise: function (e) {
    //     let objId = e.currentTarget.dataset.objid;
    //     let objType = 2;
    //     this.setData({
    //         show: 0,
    //         hidden: false,
    //         showCommentInput: false
    //     });
    //     http.post(`/praise`, {
    //         obj_id: objId,
    //         obj_type: objType
    //     }, res => {
    //         if (res.data.data.length != 0) {
    //             let sales = this.data.sales;
    //             let newSales = sales.map(item => {
    //                 if (item.id == objId) {
    //                     item.praise_number += 1;
    //                 }
    //
    //                 return item;
    //             });
    //
    //             this.setData({
    //                 sales: newSales
    //             });
    //         }
    //     });
    // },
})